/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller.actions;

import controler.formbeans.ApplicantBean;
import java.io.File;
import java.io.FileOutputStream;
import java.util.Date;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import model.logic.db.CommonWSFunctionality;
import model.logic.db.UserFunctions;
import model.logic.nondb.CommonMethods;
import model.pojo.ApplicantCompanies;
import model.pojo.Applicants;
import model.pojo.EducationLevels;
import model.pojo.Institutes;
import model.pojo.Settings;
import model.pojo.StudyFields;
import model.pojo.Vacancies;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.upload.FormFile;

/**
 *
 * @author maha
 */
public class ApplicantAction extends org.apache.struts.action.Action {

    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response) {
        ActionForward forward = new ActionForward();
        try {
            ApplicantBean applicant = (ApplicantBean) form;
            Applicants applic = new Applicants();

            applic.setUserId(applicant.getUserId());
            applic.setGender(applicant.getGender());
            applic.setFirstName(applicant.getFirstName());
            applic.setMiddleName(applicant.getMiddleName());
            applic.setLastName(applicant.getLastName());
            applic.setDateOfBirth(new Date(applicant.getBd()));
            applic.setPlaceOfBirth(applicant.getPlaceOfBirth());

            String mstatus = applicant.getMilitaryStatus();
            if (mstatus.length() != 0) {
                applic.setMilitaryStatus(applicant.getMilitaryStatus());
            } else {
                applic.setMilitaryStatus("");
            }
            applic.setMaritalyStatus(applicant.getMaritalStatus());
            applic.setDate(new Date());
            if (applicant.getChildren() >= 0) {
                applic.setNoOfChildren(applicant.getChildren());
            }
            if (applicant.getNatid().length() != 0) {
                applic.setNationalId(applicant.getNatid());
            }
            applic.setAddress(applicant.getAdd1() + " " + applicant.getAdd2());
            applic.setCountry(applicant.getCountry());
            applic.setCity(applicant.getCity());
            applic.setZipcode(applicant.getZipCode());
            applic.setPhone(applicant.getPhone());
            applic.setMobile(applicant.getMob());

            EducationLevels el = new EducationLevels();
            el.setEducationLevelId(applicant.getLevOfEd());
            applic.setEducationLevels(el);

            Institutes inst = new Institutes();
            inst.setInstituteId(applicant.getUniversity());
            applic.setInstitutes(inst);

            applic.setGradeId(applicant.getGrad());
            applic.setGraduationYear(applicant.getGradYear());

            StudyFields sf = new StudyFields();
            sf.setStudyFieldId(applicant.getFieldOfStudy());
            applic.setStudyFields(sf);

            applic.setYearsOfExperince(applicant.getYearsOfExp());

            //=================================
            Vacancies vac = new Vacancies();
            vac.setVacancyId(applicant.getVacancyId());
            applic.setVacancies(vac);
//=========================================


            FormFile file = applicant.getCvPath();
            System.out.println(file.getFileName());


            ServletContext servletContext = request.getServletContext();
            String path = "../../web/styles/upload/CVs";

            String filePath = servletContext.getRealPath(path);
            File folder = new File(filePath);
            if (!folder.exists()) {
                boolean mkdir = folder.mkdir();
                System.out.println(mkdir);
            }

            String fileName = file.getFileName();

            if (!("").equals(fileName)) {

                File newFile = new File(filePath, fileName);
                applic.setCv("styles/upload/CVs/" + newFile.getName());

                System.out.println(newFile.getCanonicalFile());
                if (!newFile.exists()) {
                    FileOutputStream fos = new FileOutputStream(newFile);
                    fos.write(file.getFileData());
                    fos.flush();
                    fos.close();
                    System.out.println("Uploaded");
                }

            }


            UserFunctions uf = new UserFunctions();
            int insertApplicant = uf.insertApplicant(applic);

            if (insertApplicant != -1) {
                if (!(applicant.getCompName1().length() <= 1) || applicant.getCompName1() == null
                        || !(applicant.getPos1().length() <= 1) || applicant.getPos1() == null
                        || !(applicant.getFrom1().length() <= 1) || applicant.getFrom1() == null
                        || !(applicant.getTo1().length() <= 1) || applicant.getTo1() == null) {
                    ApplicantCompanies c1 = new ApplicantCompanies();
                    c1.setApplicantId(insertApplicant);
                    c1.setCompanyName(applicant.getCompName1());
                    c1.setApplicantPosition(applicant.getPos1());
                    c1.setWorkFrom(new Date(applicant.getFrom1()));
                    c1.setWorkTo(new Date(applicant.getTo1()));
                    uf.insertApplicantCompanies(c1);
                }
                if (!(applicant.getCompName2().length() <= 1) || applicant.getCompName2() == null
                        || !(applicant.getPos2().length() <= 1) || applicant.getPos2() == null
                        || !(applicant.getFrom2().length() <= 1) || applicant.getFrom2() == null
                        || !(applicant.getTo2().length() <= 1) || applicant.getTo2() == null) {
                    ApplicantCompanies c2 = new ApplicantCompanies();
                    c2.setApplicantId(insertApplicant);
                    c2.setCompanyName(applicant.getCompName2());
                    c2.setApplicantPosition(applicant.getPos2());
                    c2.setWorkFrom(new Date(applicant.getFrom2()));
                    c2.setWorkTo(new Date(applicant.getTo2()));
                    uf.insertApplicantCompanies(c2);
                }
                if (!(applicant.getCompName3().length() <= 1) || applicant.getCompName3() == null
                        || !(applicant.getPos3().length() <= 1) || applicant.getPos3() == null
                        || !(applicant.getFrom3().length() <= 1) || applicant.getFrom3() == null
                        || !(applicant.getTo3().length() <= 1) || applicant.getTo3() == null) {

                    ApplicantCompanies c3 = new ApplicantCompanies();
                    c3.setApplicantId(insertApplicant);
                    c3.setCompanyName(applicant.getCompName3());
                    c3.setApplicantPosition(applicant.getPos3());
                    c3.setWorkFrom(new Date(applicant.getFrom3()));
                    c3.setWorkTo(new Date(applicant.getTo3()));
                    uf.insertApplicantCompanies(c3);
                }
            }
            ((ApplicantBean) form).setMessage("Your C.V. and Data are now Stored in our Database, we will contact you upon suitable vacancy available to your qualifications");

            forward = mapping.findForward("done");

            CommonWSFunctionality f = new CommonWSFunctionality();
            Settings settings = f.getSettings();
            String body = "<p>an applicant upload his/her C.V.</p>";
            CommonMethods.sendMail(settings.getSmtpUser(), settings.getSmtpPassword(), settings.getSmtpName(), settings.getSmtpUser(), settings.getCareerEmails(), applicant.getFirstName() + " " + applicant.getLastName() + " Uploaded C.V.", body);



        } catch (Exception e) {
            e.printStackTrace();
            ((ApplicantBean) form).setMessage("Something Wrong Occurs, May you check your Data again!!");
            forward = mapping.findForward("fail");
        }
        return forward;
    }
}
